<?php 
include_once('viper/autoloader.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<header>
		<title>VIPER: VIPER Is Personalized Experience Recommendation</title>
		<link rel="stylesheet" type="text/css" href="index.css" />
	</header>
	
	<body>
		<div id="page">
			<div id="header">
				<a href="index.php"><img id="banner" src="viperbanner.png" alt="banner" /></a>
			</div>
			<div id="content">
				<?php 
					$uid = isset($_GET['uid']) ? $_GET['uid'] : false;
					
					if(!$uid){
						//search bar
						?>
						<div id="searchbar">
							<p id="searchprompt">
								User ID:
							</p>
							<form id="thebar" action="index.php" method="get">
								<input class="bar" type="text" name="uid" />
								<br/>
								<input class="submitsearch" type="submit" value="VIPER Search" />
								
							</form>
							
				<br/><a href="howtouse.php"><h1>What is this? How do I use this?</h1></a>
						</div>
						<?php 
					}elseif (isset($_GET['similar'])){
						$similar = $_GET['similar'];
						
						$similarList = viper_server_similarFeatures::FindSimilarFeatures($uid,$similar);
						
						?>
						<div id="compositelist">
							<p id="composite">User <?php echo $uid; ?>: if you like feature<?php echo $similar; ?> consider:</p>
							<table id="compositetable">
							<tr class="compositecolumn">
								<td class="compositeheading">Feature</td>
								<td class="compositeheading">Likeness Score</td>
							</tr>
							<?php 
							$count = 0;
								foreach($similarList as $key => $value){
									if($count == 10)break;
									?>
									<tr class="compositerow">
										<td class="compositeentry"><?php echo $key; ?></td>
										<td class="compositeentry"><?php echo $value; ?></td>
									</tr>
									<?php 
									$count++;
								}
							?>
							</table>
						</div>
					<?php 
						
					}elseif (count(explode(',',$uid)) > 1){
						$list = array();
						
						$uids = explode(',',$uid);
						foreach($uids as $u){
							$list[] = $u;
						}
						$groupList = viper_server_groupFeature::GetGroupRecommendation($list);
						?>
						<div id="compositelist">
							<p id="composite">Composite Recommendation for Users 
							<?php 
							$a = array_pop($uids);
								foreach($uids as $u){
									echo $u. ', ';
								}
								echo $a;
							?> 
							</p>
							<table id="compositetable">
							<tr class="compositecolumn">
								<td class="compositeheading">Feature</td>
								<td class="compositeheading">Composite Score</td>
							</tr>
							<?php 
								foreach($groupList as $key => $value){
									?>
									<tr class="compositerow">
										<td class="compositeentry"><?php echo $key; ?></td>
										<td class="compositeentry"><?php echo $value; ?></td>
									</tr>
									<?php 
								}
							?>
							</table>
						</div>
					<?php 
						
					} elseif(isset($_GET['udata'])){
						$user = new viper_client_user($uid);
						
						if(isset($_POST['feature0'])){
							$newscore = array();
							
							for($i=0;$i<50;$i++){
								$data = $_POST['feature'.$i];
								$newscore['feature'.$i] = $data;
							}
							
							$user->setFeatureScores($newscore);
						}
						
						$featureScores = $user->getFeatureScores();
						?>
						<div id="userdatalist">
							<p id="userdata">Scores for User <?php echo $uid; ?></p>
							<form id="userdataform" method="post" action="index.php?uid=<?php echo $uid; ?>&udata=1">
							<table id="userdatatable">
							<tr class="userdatacolumn">
								<td class="userdataheading">Feature</td>
								<td class="userdataheading">Score</td>
							</tr>
							<?php 
								foreach($featureScores as $key => $value){
									?>
									<tr class="userdatarow">
										<td class="userdataentry"><?php echo $key; ?></td>
										<td class="userdataentry"><input class="userdatafield" type="text" name="<?php echo $key; ?>" value="<?php echo $value; ?>" /></td>
									</tr>
									<?php 
								}
							?>
							</table>
							<br/>
							<input class="userdatafield" type="submit" value="Save" />
							</form>
						</div>
					<?php 
					} else {
						$user = new viper_client_user($uid);
						//var_dump($user);
						$recommendFeatures = $user->getRecommendedFeatureList();
						$recommendFeatureScores = $user->getRecommendedFeatureScores();
						$globalaverage = viper_client_globalaverage::getGlobalAverage();
						?>
						<div id="recommendedlist">
							<p id="recommend">Recommended List (<a href="index.php?uid=<?php echo $uid; ?>&udata=1">user <?php echo $uid; ?></a>)</p>
							<table id="recommendtable">
							<tr class="recommendcolumn">
								<td class="recommendheading">Rank</td>
								<td class="recommendheading">Feature Name</td>
								<td class="recommendheading">VIPER Score</td>
								<td class="recommendheading">Average Score</td>
							</tr>
							
							<?php 
							if(isset($_GET['s'])){								
								$s = $_GET['s'];
								for($i=0;$i<10;$i++){
									?><tr class="recommendrow">
										<td class="recommendrank"><?php echo $s + $i + 1; ?></td>
										<?php 
									$feature = $recommendFeatures[$i+$s];
									$score = $recommendFeatureScores[$feature];
									?><td class="recommendname">
										<a href="<?php echo 'index.php?uid='.$uid.'?similar='.(substr($feature, -2, 2)); ?>"><?php echo $feature; ?></a>
									</td>
									<td class="recommenduserscore"><?php echo $score/10; ?></td>
									<td class="recommendglobalscore"><?php echo $globalaverage['feature'.($i+$s)]/100; ?></td>
									</tr>
								<?php
								}
							?>
							</table>
							<div id="pagination">
								<a href="index.php?uid=<?php echo $uid;?>&s=<?php echo $s-10; ?>" onclick="return <?php echo ($s-10) >= 0 ? 'true': 'false'; ?>"><---- Last Page </a>
								|
								<a href="index.php?uid=<?php echo $uid;?>&s=<?php echo $s+10; ?>" onclick="return <?php echo ($s+10) <= 40 ? 'true': 'false'; ?>"> Next Page ----></a>
							</div><?php
							} else {
								for($i=0;$i<10;$i++){
									?><tr class="recommendrow">
										<td class="recommendrank"><?php echo $i + 1; ?></td>
										<?php 
									$feature = $recommendFeatures[$i];
									$score = $recommendFeatureScores[$feature];
									?><td class="recommendname">
										<a href="<?php echo 'index.php?uid='.$uid.'&similar='.(str_replace('feature','',$feature)); ?>"><?php echo $feature; ?></a>
									</td>
									<td class="recommenduserscore"><?php echo $score/10; ?></td>
									<td class="recommendglobalscore"><?php echo $globalaverage['feature'.$i]/100; ?></td>
									</tr>
								<?php
								}
							?>
							</table>
							<div id="pagination">
								<a href="index.php?uid=<?php echo $uid;?>&" onclick="return false"><---- Last Page </a>
								|
								<a href="index.php?uid=<?php echo $uid;?>&s=10"> Next Page ----></a>
							</div><?php
							}
							?>
						</div> <?php 
					}
				?>
			</div>
			<div id="footer">
				<center>
The Cake is a Lie. Copyright 2011 VIPER.
				</center>
			</div>
		</div>
	</body>	
</html>